var merge = require('webpack-merge'),
    path = require('path'),
    contextPath = path.join(__dirname, '../'),
    srcPath = path.resolve(contextPath,'src'),
    distPath = path .resolve(contextPath,'dist'),
    webpack = require('webpack'),
    UglifyJSPlugin = require('uglifyjs-webpack-plugin'),
    common = require('./webpack.common.js'),
    ExtractTextPlugin = require("extract-text-webpack-plugin");


console.log("++++———————— production running ————————++++");

module.exports = merge(common, {
    devtool: 'source-map',
    output:{
        path:distPath,
        publicPath:'./',
        chunkFilename: 'static/js/bd.[name].[chunkhash:4].js', // 配合 async有利于懒加载 此项在 不要在enter中重复引入 js中 import的项
        filename:'static/js/[name].bd.[chunkhash:4].js'
    },
    module:{
        rules:[
            {
                test: /\.vue$/,
                loader: 'vue-loader',
                options: {
                    loaders:{
                        scss: ExtractTextPlugin.extract({
                           fallback:'vue-style-loader',   
                           use:'css-loader!sass-loader',
                           publicPath:'../../'
                        }),
                        css: ExtractTextPlugin.extract({
                           fallback:'vue-style-loader',   
                           use:'css-loader',
                           publicPath:'../../'
                        })
                    }

                }
            },
            {
                test:/\.css$/,
                use: ExtractTextPlugin.extract({
                    publicPath:'./',
                    fallback: "style-loader",
                    use: ["css-loader","postcss-loader"]
                }),
                exclude: /node_modules/
            },
            {
                test:/\.(png|svg|gif|jpg|woff|woff2|eot|ttf|otf)(\?\S*)?$/,
                use:[
                        {
                          //dev环境 对于output.path 即使不用file-loader也需要安装此包
                          loader:'url-loader',
                          options:{
                            limit:'8',
                            name:'static/img/[hash:6].[ext]'
                          }
                        }
                ],
                exclude: /node_modules/
            },
            {
                test:/\.(png|svg|gif|jpg)$/,
                use:[
                        {
                            loader: 'image-webpack-loader',
                        }
                ],
                exclude: /node_modules/
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin('./static/css/pages.[contenthash:6].css'), //单独分离css文件 
        new UglifyJSPlugin({
            compress: {
                warnings: false //开启压缩
            }
        }),
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify('production')
            }
        }),
        // new webpack.DllReferencePlugin({
        //    manifest: require('../manifest.json')
        // }),
        // split vendor js into its own file
        // new webpack.optimize.CommonsChunkPlugin({
        //   name: 'vendor',
        //   minChunks: function (module, count) {
        //     // any required modules inside node_modules are extracted to vendor
        //     return (
        //       module.resource &&
        //       /\.js$/.test(module.resource) &&
        //       module.resource.indexOf(
        //         path.join(__dirname, '../node_modules')
        //       ) === 0
        //     )
        //   }
        // }),
        // // extract webpack runtime and module manifest to its own file in order to
        // // prevent vendor hash from being updated whenever app bundle is updated
        // new webpack.optimize.CommonsChunkPlugin({
        //   name: 'manifest',
        //   chunks: ['vendor']
        // })
        // new webpack.optimize.OccurenceOrderPlugin(),
        //  1 vendor 打包的是enter处指定的文件 理论上 只要vendor入口的文件不变 hash不变
        //  2 事实上 即使页面新引入的不是vendor js文件 vendor的hash也变了 所以需要HashedModuleIdsPlugin
        new webpack.HashedModuleIdsPlugin(), 
        new webpack.optimize.CommonsChunkPlugin({
            name: 'common' // enter处 指定公共 bundle 的名称。 如果此项在 不要在
        })
        // new webpack.optimize.CommonsChunkPlugin({
        //     name: 'runtime'  
        //     //将webpack 的样板(boilerplate)和 manifest 提取出来。通过指定 entry 配置中未用到的名称，此插件会自动将我们需要的内容提取到单独的包中
        //     // 貌似是辅助的？
        // })
    ]
});